package action;

import Impl.userDaoImpl;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet(name = "AddUserAction")
public class AddUserAction extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------正在执行AddUserAction------------");
        request.setCharacterEncoding("utf-8");
        String method =request.getParameter("method");
        String name = request.getParameter("username");
        System.out.println(name);
        String tmpsex=request.getParameter("sex");
        System.out.println(tmpsex);
        userDaoImpl userDao=new userDaoImpl();
        int sex;
        if (tmpsex.equals("男")){
            sex=1;
        }else if (tmpsex.equals("女")){
            sex=0;
        }else{
            sex=2;
        }
        String phone =request.getParameter("mobile");
        String email =request.getParameter("email");
        String uFace=request.getParameter("uploadfile");
        User user = new User(name,sex,phone,email,uFace);
        if (method.equals("edit")){
            System.out.println("正在编辑用户信息:");
            int uid = Integer.parseInt(request.getParameter("uid"));
            user.setUid(uid);
            userDao.updateUser(user);
            System.out.println("成功更新用户信息");
        }else{
            System.out.println("正在添加用户");
            request.setCharacterEncoding("utf-8");
            userDao.addUser(user);
            System.out.println("成功添加用户:"+name);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
